<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>构造函数</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="keyword.extends.html">继承</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.paamayim-nekudotayim.html">范围解析操作符（::）</a></div>
 <div class="up"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="oop4.constructor" class="sect1">
  <h2 class="title">构造函数</h2>

  <p class="para">
   构造函数是类中的一个特殊函数，当使用 <em>new</em>
   操作符创建一个类的实例时，构造函数将会自动调用。当函数与类同名时，这个函数将成为构造函数。如果一个类没有构造函数，则调用基类的构造函数，如果有的话。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Auto_Cart&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Auto_Cart</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   上文定义了一个 Auto_Cart 类，即 Cart 类加上一个构造函数，当每次使用“new”创建一个新的 Auto_Cart
   类实例时，构造函数将自动调用并将一件商品的数目初始化为“10”。构造函数可以使用参数，而且这些参数可以是可选的，它们可以使构造函数更加有用。为了依然可以不带参数地使用类，所有构造函数的参数应该提供默认值，使其可选。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Constructor_Cart&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;买些同样的无聊老货<br /></span><span style="color: #0000BB">$default_cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;买些实在货...<br /></span><span style="color: #0000BB">$different_cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #DD0000">"20"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">17</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   也可以使用 <em>@</em>
   操作符来<em class="emphasis">抑制</em>发生在构造函数中的错误。例如 <em>@new</em>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;constructor&nbsp;of&nbsp;A.&lt;br&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;a&nbsp;regular&nbsp;function&nbsp;named&nbsp;B&nbsp;in&nbsp;class&nbsp;A.&lt;br&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;not&nbsp;a&nbsp;constructor&nbsp;in&nbsp;A.&lt;br&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;a&nbsp;regular&nbsp;function.&lt;br&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;调用&nbsp;B()&nbsp;作为构造函数<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   类 A 中的函数 B() 将立即成为类 B
   中的构造函数，虽然并不是有意如此。PHP 4
   并不关心函数是否在类 B
   中定义的，或者是否被继承来的。
  </p>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    PHP 4
    不会从派生类的构造函数中自动调用基类的构造函数。恰当地逐次调用上一级的构造函数是用户的责任。
   </p>
  </div>

  <p class="para">
   析构函数是一种当对象被销毁时，无论使用了 <span class="function"><a href="function.unset.html" class="function">unset()</a></span>
   或者简单的脱离范围，都会被自动调用的函数。PHP
   中没有析构函数。可以用 <span class="function"><a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a></span>
   来替代模拟大多数析构函数的效果。
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="keyword.extends.html">继承</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.paamayim-nekudotayim.html">范围解析操作符（::）</a></div>
 <div class="up"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
